#encoding:UTF-8
import mylib
import re
import MySQLdb
import pythonlibSB


conn = mylib.create_conn()


def get_bangxephang():
    
    try:
        results = get_url_bangxephang()
        for r in results:
    #        r = results[1]
            url,id_giai,url_time = r[0],r[1],r[2]
            date_today = mylib.get_datetime_today()
            content = mylib.get_data(url)
            if r[1] in (1,2,3,4,8):
                pos1 = content.find('style="text-align: center"')
                pos2 = content.find('</table>',pos1)
                get_str = content[pos1:pos2]
                my_pos = get_str.find('<table')
                save_content = get_str[my_pos:]
                reg = re.compile(' style=""')
                for match in reg.findall(save_content):
                    save_content = save_content.replace(match,'')
                reg = re.compile('<font color="red">')
                for match in reg.findall(save_content):
                    save_content = save_content.replace(match,'')
                if r[1] in (1,2,3,4):
                    ana_pos = save_content.find('>Đ<')
                    ana_str = save_content[ana_pos:]
    #                print ana_str
                    results = process_table(ana_str,'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
                    print "id ================ "+str(r[1])
                    results = pythonlibSB.chuanHoaHtml(results)
                    results = str(MySQLdb.escape_string(results))
                    insert_bangxephang(id_giai, url_time, results, date_today)
                    print "id ================ "+str(r[1])
                if r[1] == 8:
                    reg = re.compile('<b>')
                    for match in reg.findall(save_content):
                        save_content = save_content.replace(match,'')
                    reg = re.compile('</b>')
                    for match in reg.findall(save_content):
                        save_content = save_content.replace(match,'')
                    ana_pos = save_content.find('>Điểm<')
                    ana_str = save_content[ana_pos:]
                    results = process_table(ana_str, 'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
                    results = pythonlibSB.chuanHoaHtml(results)
                    results = str(MySQLdb.escape_string(results))
                    insert_bangxephang(id_giai, url_time, results, date_today)
                    
                
            elif r[1] == 7:
                save_content = pythonlibSB.s_string(content,'class="clgi_table2"','div')
                reg = re.compile('<b>')
                for match in reg.findall(save_content):
                    save_content = save_content.replace(match,'')
                reg = re.compile('</b>')
                for match in reg.findall(save_content):
                    save_content = save_content.replace(match,'')
                ana_pos = save_content.find('>Điểm<')
                ana_str = save_content[ana_pos:]
                results = process_table(ana_str, 'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
                results = pythonlibSB.chuanHoaHtml(results)
                results = str(MySQLdb.escape_string(results))
                insert_bangxephang(id_giai, url_time, results, date_today)
                
            elif r[1] == 5:
                print "start"
                get_str = pythonlibSB.s_string(content,'class="text-conent"','div')
                save_content = pythonlibSB.s_string(get_str,'style="font-family:','table')
                reg = re.compile(' style=""')
                for match in reg.findall(save_content):
                    save_content = save_content.replace(match,'')
                ana_pos = save_content.find('>Đ<')
                ana_str = save_content[ana_pos:]
    #                print ana_str
                results = process_table(ana_str,'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
                results = pythonlibSB.chuanHoaHtml(results)
                results = str(MySQLdb.escape_string(results))
                insert_bangxephang(id_giai, url_time, results, date_today)
                print "xong"
    except:
        print "Loi he thong"

 
def insert_bangxephang(id_giai,url_time,content,content_time):
    try:
        cmd = "CALL insert_bangxephang('%s','%s','%s','%s')"%(id_giai,url_time,content,content_time)
        print cmd
        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(cmd)
        cursor.close()
        print "insert thanh cong"
    except:
        print "Loi insert"

           
def process_table(in_str,regex):
    try:
        last_str = "<p>STT Tên đội ST HS Đ</p>"
        start = in_str.find('<tr')
        while start != -1:
    
            end = in_str.find('</tr>',start)
            if end == -1:
                break
            t = in_str[start:end]
            reg  = re.compile(regex)
            x =reg.findall(t)
            if len(x) == 1:
                new_str = "<p>"+str(x[0])+"</p>"
                last_str = last_str + new_str
            if len(x) > 1:
                save_str = "<p>"+str(x[0])+" "+str(x[1])+" "+str(x[2])+" "+str(x[len(x)-2])+" "+str(x[len(x)-1])+"</p>"
                last_str = last_str+save_str
            start = end + 1
    #    print last_str
        return last_str
    except:
        return ""




def get_url_bangxephang():
    try:
        results = []
        cmd = "SELECT * FROM tbl_bangxephang WHERE `status` = 0;"
        url_results = mylib.get_result_sql(conn, cmd)
        for u in url_results:
            url = u['url_bangxh']
            id_giai = u['id_giai']
            url_time = u['url_time']
            results.append((url,id_giai,url_time))
        
        return results
    except:
        return []
    
if __name__ == "__main__":
    get_bangxephang()